package com.yhd.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.yhd.model.PageInfo;
import com.yhd.model.ResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 基础服务类
 */
public class BaseService<M extends Mapper<T>, T> {

    @Autowired
    protected M mapper;

    /**
     * 根据实体类对象参数查询一条记录
     */
    public T selectOne(T entity) {
        return mapper.selectOne(entity);
    }

    /**
     * 实体类主键ID为查询条件
     */
    public T selectByPrimaryKey(Object id) {
        return mapper.selectByPrimaryKey(id);
    }

    /**
     *  根据实体类对象参数查询list
     */
    public List<T> select(T entity) {
        return mapper.select(entity);
    }


    public List<T> selectByExample(Object obj) {
        return mapper.selectByExample(obj);
    }

    /**
     *  根据实体类参数获取记录数
     */
    public int selectCount(T entity) {
        return mapper.selectCount(entity);
    }

    /**
     *  类条件获取记录数
     */
    public int selectCountByExample(Object obj) {
        return mapper.selectCountByExample(obj);
    }

    /**
     * 保存一个实体，null属性也会保存
     */
    public int insert(T entity) {
        return mapper.insert(entity);
    }

    /**
     * 保存一个实体，null属性不会保存
     */
    public int insertSelective(T entity) {
        return mapper.insertSelective(entity);
    }

    /**
     * 根据实体属性作为条件进行删除
     */
    public Integer delete(T entity) {
        return mapper.delete(entity);
    }

    /**
     * 根据主键ID 删除
     */
    public int deleteByPrimaryKey(Object id) {
        return mapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据主键更新属性不为null的值
     */
    public Integer updateByPrimaryKeySelective(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    /**
     * 根据obj条件 更新t参数
     */
    public Integer updateByExampleSelective(T t, Object obj) {
        return mapper.updateByExampleSelective(t, obj);
    }

    /**
     *  分页查询
     */
    public Page<T> selectByPage(Object obj, PageInfo pageInfo) {
        if (pageInfo == null) {
            pageInfo = new PageInfo().setOrderByOnly(true);
        }
        return pageBuild(pageInfo).doSelectPage(() -> {
            if (obj == null) {
                mapper.selectAll();
            } else if (obj instanceof Example || obj instanceof Example.Criteria) {
                mapper.selectByExample(obj);
            } else {
                mapper.select((T) obj);
            }
        });
    }

    /**
     *  拼接分页参数
     */
    public Page<T> pageBuild(PageInfo pageInfo) {
        Page<T> page = (Page<T>) PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getCount())
                .setOrderBy(pageInfo.getOrderBy()).setPageSizeZero(pageInfo.getPageSizeZero());
        page.setOrderByOnly(pageInfo.getOrderByOnly());
        return page;
    }

    /**
     * page对象转ResultPageList
     */
    public final static <T> ResultPage pageTransform(Page<T> page) {
        ResultPage<T> resultPageList = new ResultPage<>();
        resultPageList.setData(page.getResult());
        resultPageList.setPages(page.getPages());
        resultPageList.setTotal(page.getTotal());
        return resultPageList;
    }

}
